#!/usr/bin/env python
# -*- coding: utf-8 -*-

#need for this file
import re
import webbrowser
import urllib
import sys

#offcial
import unittest
from weibopy.auth import OAuthHandler, BasicAuthHandler
from weibopy.api import API

class allFuncs:
	last_sid = 0
	last_mid = 0
	last_usid = 0
	consumer_key= "2998871798"
	consumer_secret ="049fe45ea21fb9aa1de9c301bd5c3688"
	def __init__(self):
		""" constructor """

	def getAtt(self, key):
		try:
			return self.obj.__getattribute__(key)
		except Exception, e:
			print e
			return ''

	def getAttValue(self, obj, key):
		try:
			return obj.__getattribute__(key)
		except Exception, e:
			print e
			return ''

	def setToken(self, token, tokenSecret):
		self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
		self.auth.setToken(token, tokenSecret)
		self.api = API(self.auth)

	def setAccessToken(self, token, tokenSecret):
		self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
		self.auth.setToken(token, tokenSecret)
		self.api = API(self.auth, source=self.consumer_key)

	def auth(self, consumer_key, consumer_secret):
		self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
		auth_url = self.auth.get_authorization_url()
		webbrowser.open(auth_url)
		verifier = raw_input("输入浏览器中出现的授权码: ").strip()
		accesstoken = self.auth.get_access_token(verifier)
		self.api = API(self.auth)
		return accesstoken

	def oAuth(self, username, passwd):
		print u"			开始oauth验证"
		self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
		auth_url = self.auth.get_authorization_url()
		p = re.compile(r'oauth_token=(\w+)')
		m = p.search(auth_url)
		authtaken = m.groups()[0]
		#print authtaken
		taken = self.auth._get_request_token()
		#print taken
		params = urllib.urlencode({'oauth_token': authtaken, 'userId':username,'passwd':passwd,'action':'submit'})
		#print params
		uos = urllib.urlopen(r'http://api.t.sina.com.cn/oauth/authorize',params)
		html = uos.read()
		#print html
		p = re.compile(r'\"fb\"\>([^0-9]*[0-9]+)')
		m = p.search(html)
		#print m
		if m:
			self.pin = m.groups()[0]
			self.access_token = self.auth.get_access_token(self.pin)
			self.api = API(self.auth)
			return self.access_token
		else:
			print u"			oauth验证失败，程序退出"
			sys.exit(1)


	def user_login(self):
		timeline = self.api.user_timeline(count=1)
		for line in timeline:
			self.obj = line
			user = self.getAtt("user")
			return user.name


	def friends_timeline(self):
		timeline = self.api.friends_timeline(count=5, page=1)
		print u"			所有好友的时间线\n"
		for line in timeline:
			self.obj = line
			self.last_sid  =sid= self.getAtt("id")
			text = self.getAtt("text")
			created_at = self.getAtt("created_at")
			user = self.getAtt("user")
			user_name = user.name
			user_id = user.id
			print u"来自 " + user_name + "("+  str(user_id)  + u")  发表于 "  + str(created_at) + u" 状态的id为 : "+ str(sid)
			print text+ "\n"

	def home_timeline(self):
		timeline = self.api.friends_timeline(count=6, page=1, max_id=self.last_sid)
		for line in timeline:
			self.obj = line
			if( self.getAtt("id") == self.last_sid):
				continue
			self.last_sid  = sid = self.getAtt("id")
			text = self.getAtt("text")
			created_at = self.getAtt("created_at")
			user = self.getAtt("user")
			user_name = user.name
			user_id = user.id
			print u"来自 " + user_name + "("+  str(user_id)  + u")  发表于 "  + str(created_at) + u" 状态的id为 : "+ str(sid)
			print text+ "\n"

	def user_timeline(self, username):
		timeline = self.api.user_timeline(count=5, page=1, id = username)
		for line in timeline:
			self.obj = line
			self.last_usid = sid = self.getAtt("id")
			text = self.getAtt("text")
			created_at = self.getAtt("created_at")
			print u"   发表于 "  + str(created_at) + u" 状态的id为 : "+ str(sid)
			print text+ "\n"

	def user_timeline_continue(self, username):
		timeline = self.api.user_timeline(count=6, page=1, max_id=self.last_usid, id = username)
		for line in timeline:
			self.obj = line
			if( self.getAtt("id") == self.last_usid):
				continue
			self.last_usid  = sid = self.getAtt("id")
			text = self.getAtt("text")
			created_at = self.getAtt("created_at")
			print u"   发表于 "  + str(created_at) + u" 状态的id为 : "+ str(sid)
			print text+ "\n"

	def update(self, message):
		#message = message.encode("utf-8")
		status = self.api.update_status(message)
		self.obj = status
		sid = self.getAtt("id")
		text = self.getAtt("text")
		print u"刚刚更新的状态的id为：" + str(sid)
		print u"更新成功："+ text

	def comment(self, cid, comword):
		print str(cid), str(comword)
		comment = self.api.comment( id=cid, comment=comword)
		self.obj = comment
		cid = self.getAtt("id")
		text = self.getAtt("text")
		print u"回复成功， 这条回复的id是：" + str(cid)
		print u"回复的内容为："+ text

	def upload(self, filename, message):
		#message = message.encode("utf-8")
		status = self.api.upload(filename, status=message)
		self.obj = status
		sid = self.getAtt("id")
		text = self.getAtt("text")
		user = self.getAtt("user")
		self.obj = user
		profile_image_url  = self.getAtt("profile_image_url")
		print u"上传图片成功， 这条状态id为:" + str(sid)
		print u"图片保存的地址为：" + profile_image_url

	def mentions(self):
		comments = self.api.mentions()
		for comment in comments:
			self.obj = comment
			sid = self.getAtt("id")
			text = self.getAtt("text")
			created_at = self.getAtt("created_at")
			user = self.getAtt("user")
			user_name = user.name
			user_id = user.id
			print u"来自 " + user_name + "("+  str(user_id)  + u")  发表于 "  + str(created_at) + u" 状态的id为 : "+ str(sid)
			print text+ "\n"

	def friends(self):
		timeline = self.api.friends()
		for line in timeline:
			self.obj = line
			fid = self.getAtt("id")
			name = self.getAtt("screen_name")
			print u"好友的用户名为：" + name + u" 好友的id为："+ str(fid)

	def followers(self):
		timeline = self.api.followers()
		for line in timeline:
			self.obj = line
			fid = self.getAtt("id")
			name = self.getAtt("screen_name")
			print u"关注你的用户名为：" + name + u" 好友的id为："+ str(fid)

	def destroy_status(self, did):
		status = self.api.destroy_status(did)
		self.obj = status
		sid = self.getAtt("id")
		text = self.getAtt("text")
		print u"成功删除了id为："+ str(sid) +u" 的状态"
		print u"内容为：" +  text
